programming4us
           
 
 
Windows

Windows 7 : Controlling Services with a Script

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/1/2010 7:57:52 PM
If you want to automate service control, but you want to also control the startup type, you need to go beyond the command line and create scripts that manage your services. Windows Management Instrumentation (WMI) has a class called Win32_Service that represents a Windows service. You can return an instance of this class to work with a specific service on Windows 7. After you have the service object, you can query its current status with the State property; determine whether the service is running with the Started property; and return the service’s startup type with the StartMode property. You can also change the service state using the StartService, StopService, PauseService, and ResumeService methods.

Listing 1 presents a script that uses most of these properties and methods.

Note

You can find this script on my website at www.mcfedries.com/Windows7Unleashed.


Listing 1. A WMI Script That Toggles a Service’s State Between Started and Stopped
Option Explicit
Dim strComputer, strServiceName, intReturn
Dim objWMI, objServices, objService
'
' Get the WMI service
'
strComputer = "localhost"
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
'
' Specify the service name
'
strServiceName = "Remote Registry"
'
' Get the service instance
'
Set objServices = objWMI.ExecQuery("SELECT * FROM Win32_Service " & _
"WHERE DisplayName = '" & strServiceName & "'")
For Each objService In objServices
'
' Save the service name
'
strServiceName = objService.DisplayName
'
' Is the service started?
'
If objService.Started Then
'
' Can it be stopped?
'
If objService.AcceptStop Then
'
' Attempt to stop the service
'
intReturn = objService.StopService
'
' Check the return value
'
If intReturn <> 0 Then
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"failed to stop. The return code is " & intReturn
Else
'
' Display the current state
'
WScript.Echo "The " & strServiceName & " service is now " & _
objService.State
End If
Else
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"cannot be stopped."
End If
Else
'
' Attempt to start the service
'
intReturn = objService.StartService
'
' Check the return value
'
If intReturn <> 0 Then
'
' Display the error message
'
WScript.Echo "ERROR: The " & strServiceName & " service " & _
"failed to start. The return code is " & intReturn
Else
'
' Display the current state
'
WScript.Echo "The " & strServiceName & " service is now " & _
objService.State
End If
End If
Next
'
' Release the objects
'
Set objWMI = Nothing
Set objServices = Nothing
Set objService = Nothing


This script gets the WMI service object and uses its ExecQuery method to return an instance of the Win32_Service class by using the WHERE clause to look for a specific service name. That name was earlier stored in the strServiceName variable. In the For Each...Next loop, the script first checks to see whether the service is currently started by checking its Started property:

  • If the Started property returns True, the service is running, so we want to stop it. The script then checks the service’s AcceptStop property, which returns False for essential Windows 7 services that can’t be stopped. In this case, the script returns an error message. If AcceptStop returns True, the script attempts to stop the service by running the StopService method.

  • If the Started property returns False, the service is stopped, so we want to start it. The script attempts to start the service by running the StartService method.

The StopService and StartService methods generate the return codes shown in Table 2.

Table 2. Return Codes Generated by the StartService and StopService Methods
Return CodeDescription
0Success
1Not supported
2Access denied
3Dependent services running
4Invalid service control
5Service cannot accept control
6Service not active
7Service request timeout
8Unknown failure
9Path not found
10Service already stopped
11Service database locked
12Service dependency deleted
13Service dependency failure
14Service disabled
15Service logon failed
16Service marked for deletion
17Service no thread
18Status circular dependency
19Status — duplicate name
20Status — invalid name
21Status — invalid parameter
22Status — invalid service account
23Status — service exists
24Service already paused

For both the StopService and StartService methods, the script stores the return code in the intReturn variable and then checks to see whether it’s a number other than 0. If so, the script displays an error message that includes the return code; otherwise, the script displays the new state of the service (as given by the State property).

Other -----------------
- Windows 7 : Controlling Services at the Command Prompt
- Windows 7 : Controlling Services with the Services Snap-In
- Windows Azure : Access Control Service Usage Scenarios (part 3)
- Windows Azure : Access Control Service Usage Scenarios (part 2)
- Windows Azure : Access Control Service Usage Scenarios (part 1)
- Windows Azure : Access Control Service - Claims-Based Identity Model
- Windows Azure : Access Control Service - Concepts and Terminology
- Windows 7 : Configuring the MMC - Creating a Custom Taskpad View
- Windows 7 : Configuring the MMC - Controlling Snap-Ins with Group Policies
- Windows 7 : Configuring the MMC - Adding a Snap-In
- Configuring the Microsoft Management Console : Reviewing the Windows 7 Snap-Ins
- Windows 7 : Enabling the Shutdown Event Tracker
- Windows 7 : Increasing the Size of the Recent Documents List
- Windows 7 : Customizing the Places Bar
- Windows 7 : Customizing the Windows Security Window
- Windows 7 : Working with Group Policies
- Policing Windows 7 with Group Policies
- Windows Azure Storage : Queue Scenarios
- Windows Azure Storage : Message Operations (part 2) - Get Messages
- Windows Azure Storage : Message Operations (part 1) - Put Message
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us